package com.imooc.service.impl;

import com.imooc.mapper.StuMapper;
import com.imooc.pojo.Stu;
import com.imooc.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StuServiceImpl implements StuService {
    @Autowired
    private StuMapper stuMapper;

    @Override
    public Stu getStu(int id) {
        return stuMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(Stu stu) {
        stuMapper.insert(stu);
    }

    public void saveChild1(){
        Stu stu = new Stu();
        stu.setName("child1");
        stu.setAge(23);
        stuMapper.insert(stu);

    }

    public void saveChild2(){
        Stu stu = new Stu();
        stu.setName("child2");
        stu.setAge(23);
        stuMapper.insert(stu);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveChild() {

            saveChild1();
           // int a = 1/0;
            saveChild2();

    }


}
